package com.maple.common.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.constraintlayout.widget.ConstraintLayout
import com.maple.common.R
import com.maple.common.databinding.WidgetTitleBarViewBinding
import com.maple.common.ext.click
import com.maple.common.ext.onClick
import com.maple.common.manager.ActivityManager
import androidx.core.content.withStyledAttributes

/**
 * Tip:
 * Create by SeVen on 2024/3/21 9:46
 */
class TitleBarView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) :
    FrameLayout(context, attrs, defStyleAttr) {

    private val binding: WidgetTitleBarViewBinding

    private var onRightClickListener: (() -> Unit)? = null

    private var onLeftClickListener: (() -> Unit)? = null

    init {
        val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        binding = WidgetTitleBarViewBinding.inflate(inflater, this, true)

        binding.llBackLayer.onClick {
            if(onLeftClickListener == null){
                val activity = context as Activity
                if (ActivityManager.isActivityDestroy(context)) {
                    return@onClick
                }
                activity.finish()
            }else{
                onLeftClickListener?.invoke()
            }
        }
        initAttrs(context, attrs)
        initView()
    }

    private fun initView() {
        binding.llRightLayer.click {
            onRightClickListener?.invoke()
        }
    }


    private fun initAttrs(context: Context, attrs: AttributeSet?) {
        attrs?.let {
            context.withStyledAttributes(attrs, R.styleable.TitleBarView) {
                for (i in 0 until indexCount) {
                    when (val attr = getIndex(i)) {
                        R.styleable.TitleBarView_leftText -> {
                            val resId = getString(attr)
                            binding.tvLeftTitle.text = resId
                        }

                        R.styleable.TitleBarView_middleText -> {
                            val resId = getString(attr)
                            binding.tvMiddle.text = resId
                        }

                        R.styleable.TitleBarView_rightText -> {
                            val resId = getString(attr)
                            binding.tvRightTitle.text = resId
                        }

                        R.styleable.TitleBarView_leftIcon -> {
                            val drawable = getDrawable(attr)
                            binding.ivLeftIcon.setImageDrawable(drawable)
                        }

                        R.styleable.TitleBarView_rightIcon -> {
                            val drawable = getDrawable(attr)
                            binding.rightIvIcon.setImageDrawable(drawable)
                        }

                        R.styleable.TitleBarView_middleTextColor -> {
                            val color = getColor(attr, 0)
                            binding.tvMiddle.setTextColor(color)
                        }

                        R.styleable.TitleBarView_middleTextSize -> {
                            val size = getDimension(attr, 0f)
                            binding.tvMiddle.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
                        }

                        R.styleable.TitleBarView_leftTextColor -> {
                            val color = getColor(attr, 0)
                            binding.tvLeftTitle.setTextColor(color)
                        }

                        R.styleable.TitleBarView_rightTextColor -> {
                            val color = getColor(attr, 0)
                            binding.tvRightTitle.setTextColor(color)
                        }

                        R.styleable.TitleBarView_background -> {
                            val color = getColor(attr, 0)
                            binding.root.setBackgroundColor(color)
                        }

                        R.styleable.TitleBarView_leftTextSize -> {
                            val size = getDimension(attr, 0f)
                            binding.tvLeftTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
                        }

                        R.styleable.TitleBarView_rightTextSize -> {
                            val size = getDimension(attr, 0f)
                            binding.tvRightTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
                        }

                        R.styleable.TitleBarView_leftTextBold -> {
                            val bold = getBoolean(attr, false)
                            val paint = binding.tvLeftTitle.paint
                            paint.isFakeBoldText = bold
                        }

                        R.styleable.TitleBarView_middleTextBold -> {
                            val bold = getBoolean(attr, false)
                            val paint = binding.tvMiddle.paint
                            paint.isFakeBoldText = bold
                        }

                        R.styleable.TitleBarView_rightTextBold -> {
                            val bold = getBoolean(attr, false)
                            val paint = binding.tvRightTitle.paint
                            paint.isFakeBoldText = bold
                        }

                        R.styleable.TitleBarView_leftVisible -> {
                            val show = getBoolean(attr, true)
                            val visible = if (show) VISIBLE else INVISIBLE
                            binding.llBackLayer.visibility = visible
                            binding.tvLeftTitle.visibility = visible
                        }

                        R.styleable.TitleBarView_rightVisible -> {
                            val show = getBoolean(attr, true)
                            val visible = if (show) VISIBLE else INVISIBLE
                            binding.llRightLayer.visibility = visible
                            binding.tvRightTitle.visibility = visible
                        }

                        R.styleable.TitleBarView_middleTextVisible -> {
                            val show = getBoolean(attr, false)
                            val visible = if (show) VISIBLE else GONE
                            binding.tvMiddle.visibility = visible
                        }

                        R.styleable.TitleBarView_rightIconVisible -> {
                            val show = getBoolean(attr, false)
                            val visible = if (show) VISIBLE else GONE
                            binding.rightIvIcon.visibility = visible
                        }

                        R.styleable.TitleBarView_showDividerLine -> {
                            val show = getBoolean(attr, true)
                            val visible = if (show) VISIBLE else GONE
                            binding.dividerLine.visibility = visible
                        }
                    }
                }
            }
        }
    }

    /**
     * 设置标题
     */
    fun setMiddleText(title: String?) {
        binding.tvMiddle.text = title
    }

    /**
     * 设置标题
     */
    fun setMiddleText(resId: Int) {
        binding.tvMiddle.setText(resId)
    }

    /**
     * 设置右边是否显示
     */
    fun setRightVisible(show: Boolean) {
        val visible = if (show) VISIBLE else INVISIBLE
        binding.llRightLayer.visibility = visible
        binding.tvRightTitle.visibility = visible
    }

    /**
     * 设置右边text
     */
    fun setRightText(resId: Int) {
        binding.tvRightTitle.setText(resId)
    }

    /**
     * 设置右边text
     */
    fun setRightText(rightText: String) {
        binding.tvRightTitle.text = rightText
    }

    /**
     * 设置右边字体颜色
     */
    fun setRightTextColor(@ColorInt color: Int) {
        binding.tvRightTitle.setTextColor(color)
    }

    fun setOnRightClickListener(onRightClickListener: (() -> Unit)? = null) {
        this.onRightClickListener = onRightClickListener
    }

    fun setOnLeftClickListener(onLeftClickListener: (() -> Unit)? = null) {
        this.onLeftClickListener = onLeftClickListener
    }
}